package com.example.springbootdemo.Controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.springbootdemo.Service.IGoodsService;
import com.example.springbootdemo.entity.Goods;
import com.example.springbootdemo.utils.ApiResult;
import com.example.springbootdemo.utils.PageResult;
import com.example.springbootdemo.utils.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * goodsController
 * 
 * @author ruoyi
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询goods列表
     */
    @PassToken
    @GetMapping("/list")
    public ApiResult list(Goods goods, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize)
    {

        PageResult pageResult = goodsService.selectGoodsList(goods,pageNum,pageSize);
        return ApiResult.createBySuccess(200,pageResult,"获取成功");
    }
    @PassToken
    @GetMapping("/frontlist")
    public ApiResult list1(Goods goods){
        System.out.println(goods.getCid());
        List<Goods> list = goodsService.selectGoodsListNoPage(goods);
        return ApiResult.createBySuccess(200,list,"获取成功");
    }
    /**
     * 导出goods列表
     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Goods goods)
//    {
//        List<Goods> list = goodsService.selectGoodsList(goods);
//        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
//        util.exportExcel(response, list, "goods数据");
//    }

    /**
     * 获取goods详细信息
     */
    @PassToken
    @GetMapping(value = "/{gid}")
    public ApiResult getInfo(@PathVariable("gid") Long gid)
    {
        return ApiResult.createBySuccess(200,goodsService.selectGoodsByGid(gid),"获取成功");

    }

    /**
     * 新增goods
     */
    @PostMapping
    public ApiResult add(@RequestBody Goods goods)
    {
        int i = goodsService.insertGoods(goods);
        if(i>0){
            return ApiResult.createBySuccess(200,"","删除成功");
        }else{
            return ApiResult.createByFail(500,"删除失败");
        }
    }

    /**
     * 修改goods
     */
    @PutMapping
    public ApiResult edit(@RequestBody Goods goods)
    {
        int i = goodsService.updateGoods(goods);
        if(i>0){
            return ApiResult.createBySuccess(200,"","删除成功");
        }else{
            return ApiResult.createByFail(500,"删除失败");
        }
    }

    /**
     * 删除goods
     */

	@DeleteMapping("/{gids}")
    public ApiResult remove(@PathVariable Long[] gids)
    {
        int i = goodsService.deleteGoodsByGids(gids);
        if(i>0){
            return ApiResult.createBySuccess(200,"","删除成功");
        }else{
            return ApiResult.createByFail(500,"删除失败");
        }
    }
}
